
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QPainter>
#include <QMouseEvent>
#include <QMessageBox>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //初始化各个成员变量。
    STATE_FLAG.IsOver = 0;
    STATE_FLAG.IsStart = 0;
    STATE_FLAG.result = 0;
    map_flag = NULL;
    map_XOffset = 5;
    map_YOffset = 64;
    time_XOffset = 0;
    time_YOffset = 27;

    this->time_cost = 0;
    this->timer = new QTimer(this);
    connect(this->timer,SIGNAL(timeout()),this,SLOT(on_timeChange()));


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPixmap bmpblock;
    QPixmap bmpdigital;
    QPixmap bmpemoji;
    if(false == bmpblock.load("../src/block.bmp") || false == bmpdigital.load("../src/digital_32_25.bmp") || false == bmpemoji.load("../src/emoji.bmp"))
    {
        cout << "error:load bmp failed!" << endl;
    }
    else
    {
        cout << "info:load bmp success!" << endl;
    }
    QPainter painter(this);

    /*绘制格子的处理流程***************************************************************************Start*/
    for(int i = 0; i < this->raw; i ++)
    {
        for(int j = 0; j < this->column; j ++)
        {

            else if(1 == map_flag[i][j])
            {

            //插小旗的绘制
            else if(2 == map_flag[i][j])
            {
                painter.drawPixmap(j * 25 + map_XOffset, i * 25 + this->map_YOffset, bmpblock, 12 * 25, 0, 25, 25);
            }
            //插错旗:3
            else
            {
                painter.drawPixmap(j * 25 + map_XOffset, i * 25 + this->map_YOffset, bmpblock, 13 * 25, 0, 25, 25);
            }
        }
    }



    //游戏已结束
    if(1 == STATE_FLAG.IsOver)
    {
        //游戏失败,所有未翻开的雷全部翻开
        if(0 == STATE_FLAG.result)
        {
            for(int i = 0; i < this->raw; i ++)
            {

            }
            // 表情切换为哭
            painter.drawPixmap((this->column * 25 + 2 * map_XOffset) / 2 - 15, this->time_YOffset, bmpemoji, 2 * 31, 0, 31, 31);

        }
        //游戏胜利，切换表情，点击锁定
        else
        {
            painter.drawPixmap((this->column * 25 + 2 * map_XOffset) / 2 - 15, this->time_YOffset, bmpemoji, 1 * 31, 0, 31, 31);
        }

    }
    else
    {
        painter.drawPixmap((this->column * 25 + 2 * map_XOffset) / 2 - 15, this->time_YOffset, bmpemoji, 0, 0, 31, 31);
    }
    /*绘制格子的处理流程***************************************************************************End*/

    /*绘制剩余雷数的统计处理流程********************************************************************Start*/
    int gtemp,stemp,btemp,mine_leftTemp;
    mine_leftTemp = mine_Left < 1 ? 0 : mine_Left;
    btemp = mine_leftTemp / 100;
    stemp = mine_leftTemp / 10 - btemp * 10;
    gtemp = mine_leftTemp - btemp * 100 - stemp * 10;
    cout << "mineLeftNum: " << this->mine_Left << "  btemp: " << btemp << "  stemp: " << stemp << "  gtemp:" << gtemp << endl;
    painter.drawPixmap(0 + map_XOffset, this->time_YOffset, bmpdigital, btemp * 25, 0, 25, 32);
    painter.drawPixmap(25 + map_XOffset, this->time_YOffset, bmpdigital, stemp * 25, 0, 25, 32);
    painter.drawPixmap(50 + map_XOffset, this->time_YOffset, bmpdigital, gtemp * 25, 0, 25, 32);
    /*绘制剩余雷数的统计处理流程**********************************************************************End*/

    /*绘制时间统计的处理流程************************************************************************Start*/
    /*绘制时间统计的处理流程************************************************************************End*/
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    int ppx = event->x();
    int ppy = event->y();
    cout << "ppx: " << ppx << "  ppy: " << ppy << endl;
    cout << "xArea: " << map_XOffset << "," << 25 * this->column + map_XOffset << endl;
    cout << "yArea: " << map_YOffset << "," << 25 * this->raw + map_YOffset << endl;
    //如果点击位置不在方格区域内，点击表情进行处理，否则不处理
    if(!((ppx > map_XOffset && ppx < 25 * this->column + map_XOffset) && (ppy > map_YOffset && ppy < this->raw * 25 + map_YOffset)))
    {
        int temp_x = (this->column * 25 + 2 * map_XOffset) / 2 - 15;
        int temp_y = this->time_YOffset;
        if(ppx >= temp_x && ppx <= temp_x + 31 && ppy >= temp_y && ppy <= temp_y + 31)
        {
            on_actionRestart_triggered();
        }
        //cout << "error: not in the correct area!" << endl;
        return;
    }
    //如果游戏处于结束状态，不响应点击事件
    if(1 == STATE_FLAG.IsOver)
    {
        return;
    }
    changePosToIndex(&ppx, &ppy);
    if((Qt::LeftButton|Qt::RightButton) == event->buttons())
    {
        //如果游戏还没有开始，不响应左右双击事件
        if(0 == STATE_FLAG.IsStart)
        {
            return;
        }
        pressLeftRightButtonPorc(ppy, ppx);
    }
    else if(Qt::LeftButton == event->button())
    {
        if(0 == STATE_FLAG.IsStart)
        {
            STATE_FLAG.IsStart = 1;
            this->timer->start(1000);
        }
        pressLeftButtonProc(ppy, ppx);
    }
    else if(Qt::RightButton == event->button())
    {
        pressRightButtonProc(ppy, ppx);
    }

    //更新游戏状态
    JudgeIsOver();
    update();

}

bool MainWindow::MallocMemForMap_IsPushed(int in_raw, int in_column)
{
    if(1 > in_raw || 1 > in_column)
    {

    }
    if(NULL != map_flag)
    {

    }

    raw = in_raw;
    column = in_column;

    map_flag = new int*[raw];
    for(int j = 0; j < raw; j ++)
    {
        map_flag[j] = new int[column];
    }

    return true;
}

void MainWindow::InitMap_IsPushed()
{
    for(int i = 0 ;i < raw ;i ++)
    {

    }
}

bool MainWindow::GeneratePushedMap(int in_raw, int in_column)
{
    if(false == MallocMemForMap_IsPushed(in_raw, in_column))
    {
        cout << "error: malloc mem for map_IsPushed failed!!" << endl;
        return false;
    }

    InitMap_IsPushed();
    return true;
}

void MainWindow::changePosToIndex(int *px, int *py)
{

}

void MainWindow::recursiveFreshBlock(int raw_pos, int col_pos)
{

    for(int i = raw_s; i <= raw_e; i ++)
    {
        for(int j = col_s; j <= col_e; j ++)
        {
            if(i == raw_pos && j == col_pos)
            {
                continue;
            }
            if(0 == m.map[i][j] && 0 == map_flag[i][j])
            {
                recursiveFreshBlock(i, j);
            }
            else
            {
                map_flag[i][j] = 1;
            }
        }
    }
}

